package com.v2future.sjt.collegecheckingattendance.rollcall;

import android.content.Intent;
import android.view.View;
import android.widget.Button;

import com.google.gson.Gson;
import com.library.Constant;
import com.v2future.sjt.collegecheckingattendance.MainActivity;
import com.v2future.sjt.collegecheckingattendance.R;
import com.v2future.sjt.collegecheckingattendance.home.HomeActivity;
import com.v2future.sjt.collegecheckingattendance.rollcall.adapter.StudentsListAdapter;
import com.v2future.sjt.collegecheckingattendance.rollcall.bean.AbsentRollCallBean;
import com.v2future.sjt.collegecheckingattendance.rollcall.bean.ClassBean;
import com.v2future.sjt.collegecheckingattendance.rollcall.bean.RollCallStudentsBean;
import com.v2future.sjt.collegecheckingattendance.rollcall.bean.StudentBean;
import com.v2future.sjt.collegecheckingattendance.rollcall.bean.SubjectBean;
import com.v2future.sjt.collegecheckingattendance.utils.BaseResponseBean;
import com.v2future.sjt.collegecheckingattendance.utils.CalculateTime;
import com.v2future.sjt.collegecheckingattendance.utils.Constants;
import com.v2future.sjt.collegecheckingattendance.utils.DateUtil;
import com.v2future.sjt.collegecheckingattendance.utils.GsonUtil;
import com.v2future.sjt.collegecheckingattendance.utils.netutils.BaseRequestParams;
import com.v2future.sjt.collegecheckingattendance.utils.netutils.NetTaskListener;
import com.v2future.sjt.collegecheckingattendance.utils.netutils.NetUtils;
import com.v2future.sjt.collegecheckingattendance.widget.ListViewInScroll;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2016/2/16.
 * 缺席点名
 */
public class RollCallAbsentActivity extends MainActivity{

    public ListViewInScroll studentsLv;
    private Button saveBtn;
    List<StudentBean> dataList;

    StudentsListAdapter adapter;

    SubjectBean mSubjectBean;

    @Override
    public void init() {
        mSubjectBean = (SubjectBean) getIntent().getSerializableExtra(new SubjectBean().getClass().getSimpleName());
    }

    @Override
    public void initView() {
        setContentView(R.layout.activity_class_rollcall);
        initTitleBack(null);
        setTitleText(getString(R.string.roll_call));
        studentsLv = (ListViewInScroll) findViewById(R.id.lv_students);
        saveBtn = (Button) findViewById(R.id.btn_save);
        saveBtn.setOnClickListener(saveListener);
    }

    @Override
    public void initAfter() {
        doGetAllStudents();
    }

    /**
     * 填充数据
     */
    public void fillinView(){
        adapter = new StudentsListAdapter(dataList, this);
        studentsLv.setAdapter(adapter);
    }

    public void doGetAllStudents(){
        if (null == mSubjectBean) {
            return;
        }
        BaseRequestParams params = new BaseRequestParams(this, formatUrl(Constants.STUDENT_LIST));
        params.addQueryStringParameter("courseId", mSubjectBean.getCourseId());
        params.addQueryStringParameter("weekDay", DateUtil.getCurrentDateYMD());
        params.addQueryStringParameter("courseNumer", mSubjectBean.getCourseNumber());
        showTitleLoading();
        NetUtils.doGetRequest(this, params, new NetTaskListener<AbsentRollCallBean>() {

            @Override
            public void onSuccess(AbsentRollCallBean result) {
                dataList = result.getData();
                fillinView();
            }

            @Override
            public void onError() {

            }

            @Override
            public void onInterError(AbsentRollCallBean result) {
                tip(result.getFailReason());
            }

            @Override
            public void onNetError(int responseCode, String responseMsg, String errorResult) {
                tip(responseMsg);
            }

            @Override
            public void onCancelled() {

            }

            @Override
            public void onFinished() {
                cancelTitleLoading();
            }

            @Override
            public void onSuccessCache(AbsentRollCallBean result) {

            }
        });
    }

    public void doSaveAbsent(RollCallStudentsBean bean){
        String requestStrContent = GsonUtil.fromObjectToJsonString(bean);
        BaseRequestParams params = new BaseRequestParams(this, formatUrl(Constants.SAVE_ABSENT));
        params.addQueryStringParameter("reqStr", requestStrContent);
        NetUtils.doGetRequest(this, params, new NetTaskListener<BaseResponseBean>() {

            /**
             * 数据获取完成
             *
             * @param result
             */
            @Override
            public void onSuccess(BaseResponseBean result) {
                showOneBtnDialog(getString(R.string.notice), "保存点名信息完成", getString(R.string.iknow), new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(RollCallAbsentActivity.this, HomeActivity.class);
                        gobackWithData(intent);
                    }
                });
            }

            /**
             * 位置网络错误
             */
            @Override
            public void onError() {

            }

            /**
             * 非正常状态的数据
             *
             * @param result
             */
            @Override
            public void onInterError(BaseResponseBean result) {
                tip(result.getFailReason());
            }

            /**
             * 网络错误
             *
             * @param responseCode
             * @param responseMsg
             * @param errorResult
             */
            @Override
            public void onNetError(int responseCode, String responseMsg, String errorResult) {
                tip(responseMsg);
            }

            /**
             * 请求取消
             */
            @Override
            public void onCancelled() {

            }

            /**
             * 网络请求流程结束
             */
            @Override
            public void onFinished() {
                cancelLoadingDialog();
            }

            /**
             * 缓存获取完成
             *
             * @param result
             */
            @Override
            public void onSuccessCache(BaseResponseBean result) {

            }
        });
    }

    /**
     * 保存按钮
     */
    private View.OnClickListener saveListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (null == dataList || dataList.size() <= 0) return;
            List<RollCallStudentsBean.Student> requestDataList = new ArrayList<RollCallStudentsBean.Student>();
            //添加学生
            for (int i=0;i<dataList.size();i++) {
                RollCallStudentsBean.Student student = new RollCallStudentsBean.Student();
                student.setType(dataList.get(i).getLeaveType());
                student.setUserCode(dataList.get(i).getUserCode());
                requestDataList.add(student);
            }
            //添加课程、日期、课时
            final RollCallStudentsBean bean = new RollCallStudentsBean();
            bean.setStudents(requestDataList);
            bean.setCourseId(mSubjectBean.getCourseId());
            bean.setCourseNumer(mSubjectBean.getCourseNumber());
            bean.setWeekDay(DateUtil.getCurrentDateYMD());
            showTwoBtnDialog(getString(R.string.notice), "确定要保存点名信息吗", getString(R.string.comfirm), new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    HideDialog();
                    doSaveAbsent(bean);
                }
            }, getString(R.string.cancel), null);
        }
    };
}
